﻿--[[
Name: BrownieHelper
Developed by: Sonora (The Dragon Flight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

function AddBidFrame:Initialize(app)

    --Save the app
    self.app = app
    
    --Setup the player dropdown list width
    AddBidFramePlayerDropdownMiddle:SetWidth(185)
    AddBidFramePlayerDropdown:registerSelectionCallback(
        function(rosterName)
            if self.app:getMode()>=BROWNIEHELPER_MASTERLOOTER then
                self:updateView(self.app:getPlayerBidSetup(self.localRoster[rosterName]))
            end
        end
    )

    --Register for events
    self.app:registerEvent("BrownieHelper_BiddingOpened",
        function(playerBidSetup)
            if playerBidSetup~=false then self:OnBiddingOpened(playerBidSetup)
            else AddBidFrame:Hide() end
        end
    )

end

function AddBidFrame:OnBiddingOpened(playerBidSetup)

    --If we're in master looter mode, build a local roster for the drop down
    if self.app:getMode()>=BROWNIEHELPER_MASTERLOOTER then
        
        self.localRoster = {}
        for characterName,rosterInfo in pairs(self.app:getRoster()) do
            if characterName==rosterInfo.playername then
                self.localRoster[characterName] = characterName
            else
                self.localRoster[characterName.." ("..rosterInfo.playername..")"] = characterName
            end
        end
        
        AddBidFramePlayerDropdown:setOptions(keys(self.localRoster))
        
    end
    
    --update our view
    self:updateView(playerBidSetup)

end

function AddBidFrame:updateView(playerBidSetup)

    --Save the last playerBidSetup we saw
    self.playerBidSetup = playerBidSetup

    AddBidBidLabel:SetText("")
    AddBidItemLabel:SetText(playerBidSetup.link)
    AddBidMinbidLabel:SetText(playerBidSetup.minbid and tostring(playerBidSetup.minbid) or "You can't bid.")
    AddBidMaxbidLabel:SetText(playerBidSetup.maxbid and tostring(playerBidSetup.maxbid) or "no limit")
    AddBidInstructionsLabel:SetText(playerBidSetup.instructions)

    --If we're in master looter mode, build a local roster for the drop down
    if self.app:getMode()>=BROWNIEHELPER_MASTERLOOTER then
        
        AddBidFrameBidEditBox:Show()
        AddBidFrameBidEditBox:SetText(tostring((playerBidSetup.minbid or 0)))
        AddBidBidSlider:Hide()
        AddBidFrameOkButton:SetText("Save")
        AddBidFrameCancelButton:SetText("Cancel")
        self:Show()
        
    --If we're in clientish mode
    else
        AddBidPlayerLabel:SetText(playerBidSetup.playername)
        AddBidFrameOkButton:SetText("Submit")
        AddBidFrameCancelButton:SetText("Pass")
        AddBidFramePlayerDropdown:Hide()
        
        --Decide if we show the slider or edit box, or neither
        if playerBidSetup.maxbid==playerBidSetup.minbid then
            AddBidFrameBidEditBox:Hide()
            AddBidBidSlider:Hide()
            AddBidBidLabel:SetText(tostring(playerBidSetup.minbid))
        elseif playerBidSetup.maxbid~=nil then
            AddBidFrameBidEditBox:Hide()
            AddBidBidSlider:SetMinMaxValues(playerBidSetup.minbid, playerBidSetup.maxbid)
            AddBidBidSlider:SetValue(playerBidSetup.minbid)
            AddBidBidSliderLowerLabel:SetText(tostring(playerBidSetup.minbid))
            AddBidBidSliderUpperLabel:SetText(tostring(playerBidSetup.maxbid))
            AddBidBidSlider:Show()
        else
            AddBidFrameBidEditBox:Show()
            AddBidBidSlider:Hide()
        end
        
        if playerBidSetup.canbid and self.app:getDb().Options['ShowBidWindow'] then self:Show()
        else self:Hide()
        end
    end
    
end

function AddBidFrame:OnOkButton()
    if self.app:getMode()>=BROWNIEHELPER_MASTERLOOTER then
        
        local characterName = self.localRoster[AddBidFramePlayerDropdown:getSelected()]
        local bidValue = tonumber(AddBidFrameBidEditBox:GetText())
        
        --First attempt to not force the bid, but direct feed back to us not the player
        result = self.app:addBid(characterName,bidValue,false,true)
        
        --If that failed, ask if the ml wants to force acceptance of the bid
        if result == false then
            yesnoDialog(
                "The bid you submitted for "..characterName.." of "..tostring(bidValue).." points was rejected by the bidding system.\n\nWould you like to force the system to accept the bid?",
                function(response)
                    if response==true then
                        --Force the bid, but have feedback go to the character
                        self.app:addBid(characterName,bidValue,true,false)
                    end
                end
            )
        end
        self:Hide()
        
    else
        local bid = 0
        if self.playerBidSetup.maxbid==self.playerBidSetup.minbid then bid = self.playerBidSetup.minbid
        elseif self.playerBidSetup.maxbid~=nil then bid = AddBidBidSlider:GetValue()
        else bid = tonumber(AddBidFrameBidEditBox:GetText())
        end
        
        self.app:callSubmitBid(self.app:getMasterLooter(),{bid=bid})
    end
end

function AddBidFrame:OnCancelButton()
    self:Hide()
end

function AddBidFrame:OnItemEnter()

    --position & feed the game tool tip
    if self.playerBidSetup.link then
        GameTooltip_SetDefaultAnchor( GameTooltip, AddBidFrame )
        GameTooltip:ClearAllPoints()
        GameTooltip:SetPoint("TOPLEFT", AddBidFrame, "TOPRIGHT", 0, -15)
        GameTooltip:SetHyperlink(self.playerBidSetup.link)    
        GameTooltip:Show()
    end
    
end

function AddBidFrame:OnItemLeave()
    GameTooltip:Hide()
end
    
